package com.hezhi.project.module.notice.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hezhi.common.support.Convert;
import com.hezhi.common.utils.StringUtils;
import com.hezhi.common.utils.security.ShiroUtils;
import com.hezhi.project.module.classes.domain.Classes;
import com.hezhi.project.module.classes.mapper.ClassesMapper;
import com.hezhi.project.module.grade.domain.Grade;
import com.hezhi.project.module.grade.mapper.GradeMapper;
import com.hezhi.project.module.notice.domain.Notice;
import com.hezhi.project.module.notice.mapper.NoticeMapper;
import com.hezhi.project.module.notice.service.INoticeService;
import com.hezhi.project.module.parent.domain.Parent;
import com.hezhi.project.module.parent.mapper.ParentMapper;
import com.hezhi.project.module.recipient.domain.Recipient;
import com.hezhi.project.module.recipient.mapper.RecipientMapper;

/**
 * 公告 服务层实现
 * 
 * @author hezhi
 * @date 2018-06-25
 */
@Service
public class NoticeServiceImpl implements INoticeService
{
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private ParentMapper parentMapper;
    @Autowired
    private RecipientMapper recipientMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public Notice selectNoticeById(Integer noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(Notice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(Notice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 保存公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveNotice(Notice notice)
    {
        Integer noticeId = notice.getNoticeId();
        int rows = 0;
        if (StringUtils.isNotNull(noticeId))
        {
            notice.setUpdateBy(ShiroUtils.getLoginName());
            // 修改公告
            rows = noticeMapper.updateNotice(notice);
        }
        else
        {
            notice.setCreateBy(ShiroUtils.getLoginName());
            // 新增公告
            rows = noticeMapper.insertNotice(notice);
        }
        //如果状态为正常则将发送对象添加到接收表中
        if("1".equals(notice.getStatus())){
        	String[] str = notice.getSelectids().split("\\|");
        	recipientMapper.insertRecipientAll(notice.getNoticeId().toString(),str[0],str[1],str[2],str[3]);
        }else if("2".equals(notice.getStatus())){
        	recipientMapper.deleteRecipientByIds(new Integer[notice.getNoticeId()]);
        }
        return rows;
    }
    

    /**
     * 删除公告对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String ids)
    {
        return noticeMapper.deleteNoticeByIds(Convert.toIntArray(ids));
    }

	@Override
	public List<Map<String, Object>> findSelectList() {
		List<Map<String, Object>> list = new ArrayList<>();
		//定义角色
		Map<String, Object> s1 = new HashMap<>();
		s1.put("id", "R1");
		s1.put("pid", "根目录");
		s1.put("name", "家长");
		list.add(s1);
		//定义年级
		List<Grade> listGrade = gradeMapper.selectGradeList(new Grade());
		for(Grade g:listGrade){
			Map<String, Object> s = new HashMap<>();
			s.put("id", g.getGradeNumber());
			s.put("pid", "R1");
			s.put("name", g.getName());
			list.add(s);
		}
		//定义班级
		List<Classes> listClasses = classesMapper.selectClassesList(new Classes());
		for(Classes c:listClasses){
			Map<String, Object> s = new HashMap<>();
			s.put("id", c.getClassNumber());
			s.put("pid", c.getGradeNumber());
			s.put("name", c.getName());
			list.add(s);
		}
		//定义人员
		List<Parent> listParent = parentMapper.selectParentListToClasses();
		for(Parent p:listParent){
			Map<String, Object> s = new HashMap<>();
			s.put("id", p.getParentId());
			s.put("pid", p.getStudentNumber());
			s.put("name", p.getName());
			list.add(s);
		}
		return list;
	}

	/**
	 * 根据接收表查询通知
	 */
	@Override
	public List<Notice> selectNoticeListByRecipient(Recipient recipient) {
		return noticeMapper.selectNoticeListByRecipient(recipient);
	}

}
